﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;

namespace POSSystem.BLL
{
    public class Roles : _Roles
    {
        public static Roles GetAllActiveRoles()
        {
            Roles r = new Roles();
            r.Where.Deleted.Value = false;
            r.Where.BranchID.Value = Branches.CurrentBranchID;
            r.Query.Load();

            return r;
        }

        public static bool CheckRoleExisted(string name, int? roleID)
        {
            Roles r = new Roles();
            r.Where.Deleted.Value = false;
            r.Where.BranchID.Value = Branches.CurrentBranchID;
            r.Where.RoleName.Value = name;
            if(roleID > 0)
                r.Where.ID.Value = (int)roleID;
            r.Query.AddResultColumn(Roles.ColumnNames.ID);
            r.Query.Load();

            return r.RowCount > 0;
        }


        public static Roles GetAllActiveBranchRoles(int branchID)
        {
            Roles r = new Roles();
            r.Where.Deleted.Value = false;
            r.Where.BranchID.Value = branchID;
            r.Where.SystemAdmin.Value = true;
            r.Where.SystemAdmin.Operator = MyGeneration.dOOdads.WhereParameter.Operand.NotEqual;
            r.Query.Load();

            return r;
        }

        public static bool IsRoleInUse(int roleID)
        {
            Users u = new Users();
            u.Where.Deleted.Value = false;
            u.Where.RoleID.Value = roleID;
            u.Query.Top = 1;
            u.Query.AddResultColumn("ID");
            u.Query.Load();

            return u.RowCount > 0;
        }
    }
}
